package com.abel.bigwater.data.jnrq

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.model.JsonHelper
import com.alibaba.fastjson.annotation.JSONField
import org.joda.time.DateTime
import java.util.*

abstract class JnrqReadData : JnrqData() {

    /**
     * 表地址（ID） 8 ID0 ID1 ID2 ID3 ID4 ID5 ID6 ID7 BCD 码 ID0 为表类型，大表为 82H
     */
    var meterAddrStr: BcdString? = null

    var meterAddr: String?
        get() = meterAddrStr?.reversedBcdString()
        set(value) {
            meterAddrStr = BcdString.fromBcdString(value!!, true)
        }

    /**
     * 时间 8 ss mm hh DD MM YL YH BCD 码 秒分时日月年（低）年高
     */
    var sampleTimeStr: BcdString? = null

    @JSONField(format = JsonHelper.FULL_DATE_FORMAT)
    var sampleTime: Date? = null
        get() {
            field = sampleTimeStr?.toJnrqDateTime2()?.toDate()
            return field
        }
        set(value) {
            field = value

            sampleTimeStr = BcdString.fromJnrqServerTime2(DateTime(value))
        }

    /**
     * 水表读数 4 D0 D1 D2 D3 BCD 码 D0 小数位，低位到高位
     */
    var waterDigitsInt: Long? = null

    var waterDigits: Double? = null
        get() {
            field = if (waterDigitsInt == null) null else waterDigitsInt!! / 100.0
            return field
        }
        set(value) {
            field = value
            waterDigitsInt = if (value == null) null else (value * 100.0).toLong()
        }

    /** 状态位 2 XXH 00H BCD 吗 读表失败 XXH = 08H，
    数据校验错 XXH = 04H
     */
    var readStatusStr: BcdString? = null

    var readStatus: ReadStatusType?
        get() = if (readStatusStr?.bcdArray == null) ReadStatusType.UnknownStatus
        else ReadStatusType.findByStatus(readStatusStr!!.bcdArray!![0])
        set(value) {
            readStatusStr = BcdString(byteArrayOf(value!!.status, 0), true)
        }

    /**
     * 保留位 2 00H 00H BCD 吗 默认为 00
     */
    var reserved: BcdString? = null
}